package com.oceanstar.mapper;

import com.oceanstar.Pojo.MallOrder;
import com.oceanstar.Pojo.MallService;
import com.oceanstar.Pojo.OrderOperationLog;
import com.oceanstar.Pojo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface MallMapper {
    /**
     * 插入服务项目
     * @param service 服务对象
     */
    @Insert("INSERT INTO mall_service (name, category, description, price, unit, " +
            "service_area, contact_phone, status, create_time, update_time) " +
            "VALUES (#{name}, #{category}, #{description}, #{price}, #{unit}, " +
            "#{serviceArea}, #{contactPhone}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertService(MallService service);

    /**
     * 根据ID查询服务详情
     * @param id 服务ID
     * @return 服务信息
     */
    @Select("SELECT id, name, category, description, price, unit, " +
            "service_area as serviceArea, contact_phone as contactPhone, " +
            "status, create_time as createTime, update_time as updateTime " +
            "FROM mall_service WHERE id = #{id}")
    MallService selectServiceById(Integer id);

    /**
     * 更新服务信息
     * @param service 服务对象
     */
    @Update("UPDATE mall_service SET " +
            "name = #{name}, " +
            "category = #{category}, " +
            "description = #{description}, " +
            "price = #{price}, " +
            "unit = #{unit}, " +
            "service_area = #{serviceArea}, " +
            "contact_phone = #{contactPhone}, " +
            "status = #{status}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    void updateService(MallService service);

    /**
     * 批量删除服务项目
     * @param idList 服务ID列表
     */
    @Delete("<script>" +
            "DELETE FROM mall_service WHERE id IN " +
            "<foreach collection='list' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteServicesByIds(List<Integer> idList);

    /**
     * 按条件查询服务列表
     */
    @Select("<script>" +
            "SELECT id, name, category, description, price, unit, " +
            "service_area as serviceArea, contact_phone as contactPhone, " +
            "status, create_time as createTime, update_time as updateTime " +
            "FROM mall_service WHERE 1=1" +
            "<if test='name != null and name != \"\"'>" +
            " AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='category != null and category != \"\"'>" +
            " AND category = #{category}" +
            "</if>" +
            "<if test='minPrice != null'>" +
            " AND price &gt;= #{minPrice}" +
            "</if>" +
            "<if test='maxPrice != null'>" +
            " AND price &lt;= #{maxPrice}" +
            "</if>" +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            " ORDER BY create_time DESC " +
            " LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<MallService> selectServiceList(
            @Param("name") String name,
            @Param("category") String category,
            @Param("minPrice") Integer minPrice,
            @Param("maxPrice") Integer maxPrice,
            @Param("status") Integer status,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);

    /**
     * 按条件查询服务总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM mall_service WHERE 1=1" +
            "<if test='name != null and name != \"\"'>" +
            " AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='category != null and category != \"\"'>" +
            " AND category = #{category}" +
            "</if>" +
            "<if test='minPrice != null'>" +
            " AND price &gt;= #{minPrice}" +
            "</if>" +
            "<if test='maxPrice != null'>" +
            " AND price &lt;= #{maxPrice}" +
            "</if>" +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            "</script>")
    Long selectServiceCount(
            @Param("name") String name,
            @Param("category") String category,
            @Param("minPrice") Integer minPrice,
            @Param("maxPrice") Integer maxPrice,
            @Param("status") Integer status);

    /**
     * 查询当天订单数量（用于生成订单编号）
     * @return 当天订单数量
     */
    @Select("SELECT COUNT(*) FROM mall_order WHERE DATE(create_time) = CURDATE()")
    Integer selectTodayOrderCount();

    /**
     * 插入订单
     * @param order 订单对象
     * @return 插入的行数
     */
    @Insert("INSERT INTO mall_order (" +
            "order_id, service_id, service_name, service_category, price, unit, " +
            "service_date, time_slot, address, contact_name, contact_phone, remark, " +
            "user_id, username, user_identity, status, create_time, update_time" +
            ") VALUES (" +
            "#{orderId}, #{serviceId}, #{serviceName}, #{serviceCategory}, #{price}, #{unit}, " +
            "#{serviceDate}, #{timeSlot}, #{address}, #{contactName}, #{contactPhone}, #{remark}, " +
            "#{userId}, #{username}, #{userIdentity}, #{status}, #{createTime}, #{updateTime}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertOrder(MallOrder order);

    /**
     * 查询我的订单列表
     */
    @Select("<script>" +
            "SELECT order_id as orderId, service_id as serviceId, service_name as serviceName, " +
            "service_date as serviceDate, time_slot as timeSlot, address, contact_name as contactName, " +
            "contact_phone as contactPhone, price, status, create_time as createTime " +
            "FROM mall_order WHERE user_id = #{userId} " +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            " ORDER BY create_time DESC " +
            " LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<OrderVO> selectMyOrders(
            @Param("userId") Integer userId,
            @Param("status") Integer status,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);

    /**
     * 查询我的订单总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM mall_order WHERE user_id = #{userId} " +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            "</script>")
    Long selectMyOrdersCount(
            @Param("userId") Integer userId,
            @Param("status") Integer status);

    /**
     * 根据订单编号查询订单信息
     */
    @Select("SELECT id, order_id, user_id, status, service_date FROM mall_order WHERE order_id = #{orderId}")
    MallOrder selectOrderByOrderId(String orderId);

    /**
     * 更新订单状态
     */
    @Update("UPDATE mall_order SET status = #{status}, update_time = #{updateTime} WHERE order_id = #{orderId}")
    void updateOrderStatus(@Param("orderId") String orderId,
                          @Param("status") Integer status,
                          @Param("updateTime") Date updateTime);

    /**
     * 管理员查询订单列表
     */
    @Select("<script>" +
            "SELECT id, order_id, service_id, service_name, service_category, price, unit, " +
            "service_date, time_slot, address, contact_name, contact_phone, remark, " +
            "user_id, username, user_identity, status, create_time, update_time " +
            "FROM mall_order WHERE 1=1" +
            "<if test='orderId != null and orderId != \"\"'>" +
            " AND order_id = #{orderId}" +
            "</if>" +
            "<if test='serviceName != null and serviceName != \"\"'>" +
            " AND service_name LIKE CONCAT('%', #{serviceName}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            "<if test='beginDate != null and beginDate != \"\"'>" +
            " AND service_date &gt;= #{beginDate}" +
            "</if>" +
            "<if test='endDate != null and endDate != \"\"'>" +
            " AND service_date &lt;= #{endDate}" +
            "</if>" +
            "<if test='elderName != null and elderName != \"\"'>" +
            " AND username LIKE CONCAT('%', #{elderName}, '%')" +
            "</if>" +
            "<if test='contactPhone != null and contactPhone != \"\"'>" +
            " AND contact_phone LIKE CONCAT('%', #{contactPhone}, '%')" +
            "</if>" +
            " ORDER BY create_time DESC " +
            " LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<MallOrder> selectAdminOrders(
            @Param("orderId") String orderId,
            @Param("serviceName") String serviceName,
            @Param("status") Integer status,
            @Param("beginDate") String beginDate,
            @Param("endDate") String endDate,
            @Param("elderName") String elderName,
            @Param("contactPhone") String contactPhone,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);

    /**
     * 管理员查询订单总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM mall_order WHERE 1=1" +
            "<if test='orderId != null and orderId != \"\"'>" +
            " AND order_id = #{orderId}" +
            "</if>" +
            "<if test='serviceName != null and serviceName != \"\"'>" +
            " AND service_name LIKE CONCAT('%', #{serviceName}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            " AND status = #{status}" +
            "</if>" +
            "<if test='beginDate != null and beginDate != \"\"'>" +
            " AND service_date &gt;= #{beginDate}" +
            "</if>" +
            "<if test='endDate != null and endDate != \"\"'>" +
            " AND service_date &lt;= #{endDate}" +
            "</if>" +
            "<if test='elderName != null and elderName != \"\"'>" +
            " AND username LIKE CONCAT('%', #{elderName}, '%')" +
            "</if>" +
            "<if test='contactPhone != null and contactPhone != \"\"'>" +
            " AND contact_phone LIKE CONCAT('%', #{contactPhone}, '%')" +
            "</if>" +
            "</script>")
    Long selectAdminOrdersCount(
            @Param("orderId") String orderId,
            @Param("serviceName") String serviceName,
            @Param("status") Integer status,
            @Param("beginDate") String beginDate,
            @Param("endDate") String endDate,
            @Param("elderName") String elderName,
            @Param("contactPhone") String contactPhone);

    /**
     * 更新订单状态和备注
     */
    @Update("UPDATE mall_order SET status = #{status}, update_time = #{updateTime} " +
            "WHERE order_id = #{orderId}")
    int updateOrderStatusAndRemark(@Param("orderId") String orderId,
                                   @Param("status") Integer status,
                                   @Param("updateTime") Date updateTime);

    /**
     * 插入订单操作记录
     */
    @Insert("INSERT INTO order_operation_log (order_id, from_status, to_status, " +
            "handle_remark, operator_id, operator_name, create_time, update_time) VALUES (" +
            "#{orderId}, #{fromStatus}, #{toStatus}, #{handleRemark}, " +
            "#{operatorId}, #{operatorName}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertOperationLog(OrderOperationLog log);
}
